# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from fiction_Scrapy.items import FictionScrapyItem
#标题处理
#内容处理
#判断文章是否章节连续写入
class FictionScrapyPipeline(object):

    def __init__(self):
        self.file = open('file.txt', 'a', encoding='utf-8')

    def process_item(self, item):

        title = item['title']
        content = item['content']

        if isinstance(item, FictionScrapyItem):
            self.file.write('##'+title+ '\n')
            for line in content:
                line = line.strip()
                self.file.write('  '+line+ '\n')

        self.file.flush()
        return item

    def close_spider(self):
        self.file.close()
